package me.landery.modules.sinco.service.impl;

import lombok.RequiredArgsConstructor;
import me.landery.modules.sinco.domain.Order;
import me.landery.modules.sinco.repository.OrderRepository;
import me.landery.modules.sinco.service.OrderService;
import me.landery.modules.sinco.service.dto.OrderQueryCriteria;
import me.landery.modules.sinco.service.mapstruct.OrderMapper;
import me.landery.modules.system.domain.Job;
import me.landery.modules.system.repository.JobRepository;
import me.landery.modules.system.service.dto.JobQueryCriteria;
import me.landery.modules.system.service.mapstruct.JobMapper;
import me.landery.utils.PageUtil;
import me.landery.utils.QueryHelp;
import me.landery.utils.RedisUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author cc.w
 * @Date 2022/6/7 16:55
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "order")
public class OrderServiceImpl implements OrderService {

    private final OrderRepository orderRepository;
    private final OrderMapper orderMapper;
    private final RedisUtils redisUtils;

    @Override
    public Map<String, Object> queryAll(OrderQueryCriteria criteria, Pageable pageable) {
        Page<Order> page = orderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(orderMapper::toDto).getContent(),page.getTotalElements());
    }

    @Override
    public void save(Order order) {
        orderRepository.save(order);
    }
}
